package top.continew.admin.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.generator.model.req.ImportBatchReq;
import top.continew.admin.generator.model.resp.ImportBatchDetailResp;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import top.continew.admin.generator.mapper.ImportBatchMapper;
import top.continew.admin.generator.model.entity.ImportBatchDO;
import top.continew.admin.generator.model.query.ImportBatchQuery;
import top.continew.admin.generator.model.resp.ImportBatchResp;
import top.continew.admin.generator.service.ImportBatchService;

/**
 * 导入批次业务实现
 *
 * @author jiangjun
 * @since 2025/01/20 10:00
 */
@Service
@RequiredArgsConstructor
public class ImportBatchServiceImpl extends BaseServiceImpl<ImportBatchMapper, ImportBatchDO, ImportBatchResp, ImportBatchDetailResp, ImportBatchQuery, ImportBatchReq> implements ImportBatchService {

    @Override
    public String createBatch(String fileName, Integer totalCount) {
        ImportBatchDO batch = new ImportBatchDO();
        batch.setBatchName("导入批次_" + DateUtil.format(DateUtil.date(), "yyyyMMdd_HHmmss"));
        batch.setFileName(fileName);
        batch.setTotalCount(totalCount);
        batch.setSuccessCount(0);
        batch.setFailCount(0);
        batch.setStatus("processing");
        
        this.save(batch);
        return String.valueOf(batch.getId());
    }

    @Override
    public void updateBatchStatus(String batchId, String status, Integer successCount, Integer failCount, String remark,Integer totalCount) {
        ImportBatchDO batch = this.getById(Long.valueOf(batchId));
        if (batch != null) {
            batch.setStatus(status);
            batch.setSuccessCount(successCount);
            batch.setFailCount(failCount);
            batch.setRemark(remark);
            batch.setTotalCount(totalCount);
            this.updateById(batch);
        }
    }

    @Override
    public Object getBatchOptions() {
        return this.list().stream()
                .map(batch -> {
                    java.util.Map<String, Object> option = new java.util.HashMap<>();
                    option.put("value", String.valueOf(batch.getId()));
                    option.put("label", batch.getBatchName());
                    return option;
                })
                .collect(java.util.stream.Collectors.toList());
    }
}